class BookInfosController < ApplicationController
  # GET /book_infos
  # GET /book_infos.json
  helper_method :sort_column, :sort_direction
  def index
    @book_infos = BookInfo.order(sort_column + " " + sort_direction).search(params[:search])
    #if params[:search]
    #  @book_infos = BookInfo.order(sort_column + " " + sort_direction).se#.find(:all, :conditions => ['name LIKE ?', "%#{params[:search]}%"])
    #else
    #  @book_infos = BookInfo.order(sort_column + " " + sort_direction)#.find(:all, :conditions => ['name LIKE ?', "%#{params[:search]}%"])
    #end
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @book_infos }
    end
  end

  # GET /book_infos/1
  # GET /book_infos/1.json
  def show
    @book_info = BookInfo.find(params[:id])
    @relativeBooks = BookInfo.findRelativeBook(params[:id], @book_info.category_id)

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @book_info, json: @relativeBooks }
    end
  end

  # GET /book_infos/new
  # GET /book_infos/new.json
  def new
    @book_info = BookInfo.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @book_info }
    end
  end

  # GET /book_infos/1/edit
  def edit
    @book_info = BookInfo.find(params[:id])
  end

  # POST /book_infos
  # POST /book_infos.json
  def create
    @book_info = BookInfo.new(params[:book_info])

    respond_to do |format|
      if @book_info.save
        format.html { redirect_to @book_info, notice: 'Book info was successfully created.' }
        format.json { render json: @book_info, status: :created, location: @book_info }
      else
        format.html { render action: "new" }
        format.json { render json: @book_info.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /book_infos/1
  # PUT /book_infos/1.json
  def update
    @book_info = BookInfo.find(params[:id])

    respond_to do |format|
      if @book_info.update_attributes(params[:book_info])
        format.html { redirect_to @book_info, notice: 'Book info was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @book_info.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /book_infos/1
  # DELETE /book_infos/1.json
  def destroy
    @book_info = BookInfo.find(params[:id])
    @book_info.destroy

    respond_to do |format|
      format.html { redirect_to book_infos_url }
      format.json { head :no_content }
    end
  end
  
   private
  
  def sort_column
    BookInfo.column_names.include?(params[:sort]) ? params[:sort] : "name"
  end
  
  def sort_direction
    %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
  end
  
end
